import axios from 'axios';
import { message } from '../axios/message';
import router from '../router/index'
import store from '../store/index'
const requests = axios.create({
    baseURL: "http://10.48.43.189:8094",
    headers: {
        Authorization: `${localStorage.getItem("token")}`
    }
})

requests.interceptors.request.use(config => {
    // console.log(config);
   
    config.headers['Authorization'] = `${localStorage.getItem("token")}`;
   config.headers['checkedPassword'] = store.state.checkedPassword
    return config;
}, error => {
    console.log(error);
    return Promise.reject(new Error("faile"));
})

requests.interceptors.response.use(response => {
    if (response.data.status == 401) {
        message.error("用户未登录")
        router.push({
            path: '/lockPage'

        });
    } else if (response.data.status == 500) {
        message.error(response.data.desc)
    } else if (response.data.status == 402) {
        message.error(response.data.desc)
        store.state.showPassword = true
        console.log("进行弹窗验证身份");
    }
    else if (response.data.status == 200) {
        store.state.checkedPassword = 'false'
        if (response.data.desc != null) {
            message({
                showClose: true,
                message: response.data.desc,
                type: 'success'
            });
        }
    }

    return response;
}, error => {
    console.log(error);

    if (error.response.status === 401) {
        router.push({
            path: '/lockPage'
        });
    }
    return Promise.reject(new Error("faile"));
})

export default requests;